import 'package:amap_base/amap_base.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:hbtrans_flutter/beans/areadetail_bean.dart';
import 'package:hbtrans_flutter/utils/mydio.dart';

import '../apis.dart';
import '../const.dart';

//const polylineList = const [
//  LatLng(40.999401, 116.135972),
//  LatLng(40.898323, 116.057694),
//  LatLng(40.900430, 116.265061),
//  LatLng(40.955192, 116.140092),
//];


const titles = const [
  '行政区域',
  '培训机构编号',
  '驾培机构名称',
  '教学区域面积(平方米)',
  '教学区域编号',
  '培训车型',
  '教学区域地址',
  '可容纳车辆数',
  '以投放车辆数',
  '更新时间',
  '审核状态',
  '审核时间',
  '不同意原因',
];
var isvalid = '';
var inscode = '';

class AreaDetail extends StatefulWidget {
  final id;

  const AreaDetail({Key key, this.id}) : super(key: key);

  @override
  _AreaDetailState createState() => _AreaDetailState();
}

class _AreaDetailState extends State<AreaDetail> {
  AMapController _controller;
  bool unShow = true;
  var loadingDialog;
  bool getLine = false;
  List<LatLng> polylineList = List();

  var datas = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getLine = false;
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  _setList() {
    List<Widget> list = List();
    for (var i = 0; i < titles.length; i++) {
      list.add(Container(
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Text(
              titles[i],
              style: TextStyle(fontSize: 14, color: Color(0xff333333)),
            ),
            Text(
              datas.length == 0 ? '' : datas[i].toString(),
              style: TextStyle(fontSize: 14, color: Color(0xff333333)),
            )
          ],
        ),
      ));
    }

    return list;
  }

  _getById() async {
    getLine = true;
    print('id:${widget.id}');
    Map data = Map<String, dynamic>();
    data["id"] = widget.id;
    MyDio.getInstance().get(Apis.getById, (data) {
      print(data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }

      AreadetailBean bean = AreadetailBean.fromJson(data);

      String polygon = bean.polygon;
      var polygons = polygon.split(';');
      for (String s in polygons) {
        var loc = s.split(',');
        polylineList.add(LatLng(double.parse(loc[1]), double.parse(loc[0])));
      }
      isvalid = bean.isvalid;
      if (isvalid == '1') {
        isvalid = '解除备案';
      } else if (isvalid == '2') {
        isvalid = '审核';
      }
      inscode = bean.id.toString();
      setState(() {
        if (polylineList.length == 0) {
          return;
        }
        _controller.addPolyline(
          PolylineOptions(
            latLngList: polylineList,
            color: Colors.red,
            isDottedLine: false,
            isGeodesic: true,
            dottedLineType: PolylineOptions.DOTTED_LINE_TYPE_CIRCLE,
            width: 10,
          ),
        );
        _controller.setZoomLevel(15);
        _controller.changeLatLng(polylineList[0]);
        datas.clear();
        datas.add(bean.districtName);
        datas.add(bean.inscode);
        datas.add(bean.schName);
        datas.add(bean.area);
        datas.add(bean.seq);
        datas.add(bean.vehicletype);
        datas.add(bean.address);
        datas.add(bean.totalvehnum);
        datas.add(bean.curvehnum);
        datas.add(bean.updatetime);
        datas.add(bean.isvalid);
        datas.add(bean.checktime);
        datas.add(bean.reason);
      });
    }, params: data);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Container(
            child: AMapView(
                onAMapViewCreated: (controller) {
                  _controller = controller;
                  if (!getLine) {
                    _getById();
                  }
                },
                amapOptions: AMapOptions(
                    rotateGesturesEnabled: false,
                    compassEnabled: true,
                    zoomControlsEnabled: false,
                    logoPosition: LOGO_POSITION_BOTTOM_CENTER,
                    zoomGesturesEnabled: true,
                    camera: CameraPosition(
                      target: polylineList.length == 0
                          ? LatLng(0, 0)
                          : polylineList[0],
                      zoom: 1,
                    ))),
          ),
          Positioned(
            bottom: 0,
            child: Row(
              mainAxisSize: MainAxisSize.max,
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: <Widget>[
                _setButton(isvalid),
              ],
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                margin: EdgeInsets.fromLTRB(30, 50, 30, 0),
                color: Colors.white,
                height: 42,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    IconButton(
                      onPressed: () {
                        Navigator.pop(context);
                      },
                      icon: Icon(Icons.arrow_back_ios),
                      iconSize: 24,
                      color: Colors.black,
                    ),
                    GestureDetector(
                        onTap: () {
                          setState(() {
                            unShow = !unShow;
                          });
                        },
                        child: Text(
                          '教学区域基本信息',
                          style:
                              TextStyle(color: Color(0xff333333), fontSize: 14),
                        ))
                  ],
                ),
              ),
              Offstage(
                offstage: unShow,
                child: Container(
                  height: 550,
                  color: Colors.white,
                  margin: EdgeInsets.fromLTRB(30, 0, 30, 0),
                  child: ListView(
                    padding: EdgeInsets.all(0),
                    children: _setList(),
                  ),
                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  _setButton(String string) {
    if (string == '0' || string == '4') {
      return Container();
    } else {
      return Container(
        height: 42,
        width: MediaQuery.of(context).size.width,
        child: FlatButton(
          color: Color(0xff23AFDC),
          onPressed: () async {
            if (isvalid == '审核') {
              _auditOfPandingAction();
            } else if (isvalid == '解除备案') {
              _removeRecordAction();
            }
          },
          child: Text(
            string,
            style: TextStyle(fontSize: 16, color: Colors.white),
          ),
        ),
      );
    }
  }

  _removeRecordAction() {
    showCupertinoDialog(
        context: context,
        builder: (BuildContext context) {
          return new CupertinoAlertDialog(
            title: new Text(
              "提示",
            ),
            content: new Text("是否解除备案"),
            actions: <Widget>[
              new Container(
                decoration: BoxDecoration(
                    border: Border(
                        right: BorderSide(color: Colors.white70, width: 1.0),
                        top: BorderSide(color: Colors.white70, width: 1.0))),
                child: FlatButton(
                  child: new Text("确定"),
                  onPressed: () {
                    _postButton('0');
                    Navigator.pop(context);
                  },
                ),
              ),
              new Container(
                decoration: BoxDecoration(
                    border: Border(
                        top: BorderSide(color: Colors.white70, width: 1.0))),
                child: FlatButton(
                  child: new Text("取消"),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              )
            ],
          );
        });
  }

  _auditOfPandingAction() {
    showCupertinoDialog(
        context: context,
        builder: (BuildContext context) {
          return new CupertinoAlertDialog(
            title: new Text(
              "提示",
            ),
            content: new Text("确认审核"),
            actions: <Widget>[
              new Container(
                decoration: BoxDecoration(
                    border: Border(
                        right: BorderSide(color: Colors.white70, width: 1.0),
                        top: BorderSide(color: Colors.white70, width: 1.0))),
                child: FlatButton(
                  child: new Text("审核通过"),
                  onPressed: () {
                    _postButton('1');
                    Navigator.pop(context);
                  },
                ),
              ),
              new Container(
                decoration: BoxDecoration(
                    border: Border(
                        top: BorderSide(color: Colors.white70, width: 1.0))),
                child: FlatButton(
                  child: new Text("审核不通过"),
                  onPressed: () {
                    _postButton('4');
                    Navigator.pop(context);
                  },
                ),
              ),
              new Container(
                decoration: BoxDecoration(
                    border: Border(
                        top: BorderSide(color: Colors.white70, width: 1.0))),
                child: FlatButton(
                  child: new Text("取消"),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                ),
              )
            ],
          );
        });
  }

  _postButton(String string) {
    Map data = Map<String, dynamic>();
    data['id'] = inscode;
    data['isvalid'] = string;
    MyDio.getInstance(url: Const.BASEURL).post(Apis.bussregUpdate, (data) {
      print(data);
      if (loadingDialog != null) {
        loadingDialog = null;
        Navigator.pop(context);
      }
      _getById();
    }, params: data);
  }
}
